package com.zzj.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzj.entity.ChatFriends;
import com.zzj.mapper.ChatFriendsMapper;
import com.zzj.model.dto.chat.ChatFriendsCreateDTO;
import com.zzj.service.IChatFriendsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 好友表 服务实现类
 * </p>
 *
 * @author zzj
 * @since 2023-05-13
 */
@Service
public class ChatFriendsServiceImpl extends ServiceImpl<ChatFriendsMapper, ChatFriends> implements IChatFriendsService {

    @Override
    public boolean mutualCreateFriend(ChatFriendsCreateDTO in) {
        ChatFriends chatFriends1 = new ChatFriends();
        chatFriends1.setFromName(in.getFromName());
        chatFriends1.setFromUsername(in.getFromUsername());
        chatFriends1.setFromAvatar(in.getFromAvatar());
        chatFriends1.setToName(in.getToName());
        chatFriends1.setToUsername(in.getToUsername());
        chatFriends1.setToAvatar(in.getToAvatar());
        boolean save1 = this.save(chatFriends1);
        ChatFriends chatFriends2 = new ChatFriends();
        chatFriends2.setFromName(in.getToName());
        chatFriends2.setFromUsername(in.getToUsername());
        chatFriends2.setFromAvatar(in.getToAvatar());
        chatFriends2.setToName(in.getFromName());
        chatFriends2.setToUsername(in.getFromUsername());
        chatFriends2.setToAvatar(in.getFromAvatar());
        boolean save2 = this.save(chatFriends2);
        return save1 && save2;
    }

    @Override
    public boolean isFriend(String fromUsername, String toUsername) {

        ChatFriends one = this.getOne(Wrappers.<ChatFriends>lambdaQuery()
                        .eq(ChatFriends::getFromUsername, fromUsername)
                        .eq(ChatFriends::getToUsername, toUsername),
                false);
        return one != null;
    }

    @Override
    public ChatFriends findChatFriend(String fromUsername, String toUsername) {
      return this.getOne(Wrappers.<ChatFriends>lambdaQuery()
                        .eq(ChatFriends::getFromUsername, fromUsername)
                        .eq(ChatFriends::getToUsername, toUsername),
                false);
    }
}
